//
//  demo02ViewController.m
//  demo30导航栏透明
//
//  Created by bing on 15/11/10.
//  Copyright © 2015年 bing. All rights reserved.
//

#import "demo02ViewController.h"
#import "demo03ViewController.h"

@interface demo02ViewController ()
@property (nonatomic, weak) UIView *v;
@end

@implementation demo02ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"demo 02";
    // 滑动时隐藏
    // self.navigationController.hidesBarsOnSwipe = YES;
    
    // 获取导航栏的背景色
    for (UIView *sub in self.navigationController.navigationBar.subviews) {
        if ([sub isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
            _v = sub;
            break;
        }
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    UIImage *image = [self buttonImageFromColor:[UIColor clearColor]];
    // 透明

    /*
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = YES;
     */
//    self.navigationController.navigationBar.shadowImage = [UIImage new];
    
    
    // 使导航栏切换渐变
    [UIView animateWithDuration:0.3 animations:^{
        _v.alpha = 0.0;
    } completion:^(BOOL finished) {
        UIImage *image = [self buttonImageFromColor:[UIColor clearColor]];
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage new];
        _v.alpha = 1.0;
    }];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    /*
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
     */
     
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    demo03ViewController *vc = [demo03ViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
//通过颜色来生成一个纯色图片
- (UIImage *)buttonImageFromColor:(UIColor *)color{
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
@end
